package com.example.demo.controller;

import com.example.demo.domain.receive.RewNewBookReceive;
import com.example.demo.domain.result.BookDetailResult;
import com.example.demo.domain.result.FindBookResult;
import com.example.demo.domain.result.CertificateInformationResult;
import com.example.demo.domain.result.ReNewBookResult;
import com.example.demo.exception.ParamErrorException;
import com.example.demo.exception.PasswordErrorException;
import com.example.demo.result.ApiResult;
import com.example.demo.service.LibraryService;
import com.example.demo.service.RenewBookService;
import com.google.gson.Gson;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

/**
 * @author lizijian
 */
@RestController
@RequestMapping("/School/library")
@Slf4j
public class LibraryController {
    private static final Gson JSON=new Gson();

    @Autowired
    private LibraryService libraryService;

    @ApiOperation(value = "使用教务系统账号密码查询图书馆信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "username", value = "教务系统账号", required = true, paramType = "query"),
            @ApiImplicitParam(name = "password", value = "教务系统密码", required = true, paramType = "query"),})
    @RequestMapping("/info")
    public ApiResult<CertificateInformationResult> library(@NotBlank String username, @NotBlank String password) throws IOException, PasswordErrorException {
        CertificateInformationResult certificateInformationResult = LibraryService.getLibraryInfo(username, password);
        return ApiResult.success(certificateInformationResult);
    }

    @ApiOperation(value = "续借图书")
    @ApiImplicitParams({@ApiImplicitParam(name = "username", value = "教务系统账号", required = true, paramType = "query"),
            @ApiImplicitParam(name = "password", value = "教务系统密码", required = true, paramType = "query"),
            @ApiImplicitParam(name = "checks", value = "检查,json格式字符串\n"
                + "[{\"check\":\"677B0DAB\",\"barCode\":\"01292522\"},{\"check\":\"9ABFCCDB\",\"barCode\":\"01501954\"}]", required = true, paramType = "query")})
    @RequestMapping("/book/renew")
    public ApiResult<List<ReNewBookResult>> renewBook(@NotBlank String username, @NotBlank String password, @NotBlank String checks) throws TesseractException, IOException, PasswordErrorException {
        RewNewBookReceive[] rewNewBookReceives = JSON.fromJson(checks, RewNewBookReceive[].class);
        return ApiResult.success(libraryService.renewBook(username,password,rewNewBookReceives));
    }


    @ApiOperation(value = "查找图书")
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "书名", required = true, paramType = "query"),
            @ApiImplicitParam(name = "password", value = "类型", required = true, paramType = "query"),
            @ApiImplicitParam(name = "checks", value = "页数", required = true, paramType = "query")})
    @GetMapping("/book/find")
    public ApiResult<FindBookResult> findBook(@NotBlank String name, @NotBlank String type,Integer page) throws ParamErrorException, IOException {
        return ApiResult.success(libraryService.findBook(type,name,page));
    }

    @ApiOperation(value = "查看图书详情")
    @ApiImplicitParam(name = "url", value = "图书地址", required = true, paramType = "query")
    @GetMapping("/book/detail")
    public ApiResult<BookDetailResult> bookDetail(@NotBlank  String url) throws IOException {
        return ApiResult.success(libraryService.getBookDetail(url));
    }

}
